﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Diagnostics;

namespace ipcamehotKey
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            IPCameHook.Hooks.SetHook();
            IPCameHook.Hooks.OnHook += new EventHandler(Hooks_OnHook);
            IPCameHook.Hooks.SendRequest = Request;

            //Загружаем настройки
            //Properties.Settings.Default.Reload();

            tbIPCame1.Text = Properties.Settings.Default.IPCame1;
            tbIPCame2.Text = Properties.Settings.Default.IPCame2;
            tbIPCame3.Text = Properties.Settings.Default.IPCame3;

            tbUserName.Text = Properties.Settings.Default.UserName;
            tbPswd.Text = Properties.Settings.Default.Password;

        }

        void Hooks_OnHook(object sender, EventArgs e)
        {            
            niTray.BalloonTipText = (string)sender;
            niTray.ShowBalloonTip(1);
        }

        public void Request(object obj)
        {
            object[] _data = (object[])obj;
            
            string url = (string)(_data[0]);
            string msg = (string)(_data[1]);

            try
            {
                if (url != "")
                {

                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                    req.Timeout = 10000;
                    req.Credentials = new NetworkCredential(ipcamehotKey.Properties.Settings.Default.UserName, ipcamehotKey.Properties.Settings.Default.Password);
                    WebResponse wr = req.GetResponse();
                    wr.Close();

                    Hooks_OnHook((object)msg + "\r\n" + "Выполнено", new EventArgs());
                }
            }
            catch (Exception Ex)
            {
                Hooks_OnHook((object)msg + "\r\n" + Ex.Message, new EventArgs());
            }               
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            IPCameHook.Hooks.UnHook();                       
        }

        private void niTray_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = ! this.Visible;
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            this.Visible = false;           
        }

        private void tbIPCame1_TextChanged(object sender, EventArgs e)
        {
            //bSave_Click(bSave,new EventArgs());
        }

        private void bSave_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.IPCame1 = tbIPCame1.Text;
            Properties.Settings.Default.IPCame2 = tbIPCame2.Text;
            Properties.Settings.Default.IPCame3 = tbIPCame3.Text;

            Properties.Settings.Default.UserName = tbUserName.Text;
            Properties.Settings.Default.Password = tbPswd.Text;

            //Сохраняем настройки           
            Properties.Settings.Default.Save();

            niTray.BalloonTipText = "Сохранение настроек";
            niTray.ShowBalloonTip(1);
        }

        private void bHide_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void bExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsmiShow_Click(object sender, EventArgs e)
        {
            this.Visible = ! this.Visible;
        }

        private void tsmiClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsmiShow_Paint(object sender, PaintEventArgs e)
        {
            if (this.Visible) { tsmiShow.Text = "Скрыть"; }
            else { tsmiShow.Text = "Показать"; }
        }

        

        private void tsmiCame1On_Click(object sender, EventArgs e)
        {
            CameOn( @"http://" + ipcamehotKey.Properties.Settings.Default.IPCame1 + "/dev/gpioCtrl.cgi?out1=1");
        }
        private void tsmiCame2On_Click(object sender, EventArgs e)
        {
            CameOn(@"http://" + ipcamehotKey.Properties.Settings.Default.IPCame2 + "/dev/gpioCtrl.cgi?out1=1");
        }   
        private void tsmiCame3On_Click(object sender, EventArgs e)
        {
            CameOn(@"http://" + ipcamehotKey.Properties.Settings.Default.IPCame3 + "/dev/gpioCtrl.cgi?out1=1");
        }
        
        
        private void CameOn(string url)
        {
            try
            {
                if (url != "")
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                    req.Timeout = 10000;
                    req.Credentials = new NetworkCredential(ipcamehotKey.Properties.Settings.Default.UserName, ipcamehotKey.Properties.Settings.Default.Password);
                    req.GetResponse();

                    niTray.BalloonTipText = "Управление выполнено\r\n";
                    niTray.BalloonTipText += url;
                }
            }
            catch (Exception Ex)
            {
                niTray.BalloonTipText = "Управление не выполнено - " + Ex.Message+"\r\n";
                niTray.BalloonTipText += url;
            }
            niTray.ShowBalloonTip(1);
        }

        private void tsmiCame1Off_Click(object sender, EventArgs e)
        {
            CameOn(@"http://" + ipcamehotKey.Properties.Settings.Default.IPCame1 + "/dev/gpioCtrl.cgi?out1=0");
        }

        private void tsmiCame2Off_Click(object sender, EventArgs e)
        {
            CameOn(@"http://" + ipcamehotKey.Properties.Settings.Default.IPCame1 + "/dev/gpioCtrl.cgi?out1=0");
        }

        private void tsmiCame3Off_Click(object sender, EventArgs e)
        {
            CameOn( @"http://" + ipcamehotKey.Properties.Settings.Default.IPCame1 + "/dev/gpioCtrl.cgi?out1=0");

        }

            
    }
}